package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;

import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;

import com.itheima.reggie.mapper.SetmealMapper;

import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional //事务注解
    public void addSetmeal(@RequestBody SetmealDto setmealDto) {
        //保存套餐数据到Setmeal表中
        this.save(setmealDto);
        //获取集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //遍历集合
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息,操作setmeal_dish，执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据id删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);
        if(count > 0){
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);

        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据----setmeal_dish
        setmealDishService.remove(lambdaQueryWrapper);
    }

    /**
     * 根据id查看套餐信息和关系的菜品数据
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithsetmeal(Long id) {
        //根据套餐id查询套餐数据
        Setmeal setmeal = this.getById(id);
        //根据套餐id查询套餐对应的菜品数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        //查询菜品数据
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        //创建SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        //把setmeal信息拷贝到setmealDto
        BeanUtils.copyProperties(setmeal,setmealDto);
        //设置setmealDto的菜品信息
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 更新套餐信息
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithSetmeal(SetmealDto setmealDto) {
        //根据id更新 setmeal 套餐表数据
        this.updateById(setmealDto);
        //清理当前套餐对应的菜品数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        //删除菜品数据
        setmealDishService.remove(queryWrapper);
        //添加当前提交过来的新的菜品数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //遍历菜品集合
        setmealDishes = setmealDishes.stream().map((item)->{
            //设置套餐id
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

}
